package cn.by.wms.service;

import cn.by.wms.dao.InventoryBatchDao;
import cn.by.wms.dao.InventoryDao;
import cn.by.wms.dao.IoItemDao;
import cn.by.wms.dto.InventoryOptionDTO;
import cn.by.wms.entity.*;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.smart.core.exception.BizException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.ZoneId;
import java.util.Date;
import java.util.List;

@Service("in")
public class InInventoryOptionServiceImpl implements IInventoryOptionService {
    @Autowired
    private IoItemDao ioItemDao;
    @Autowired
    private InventoryDao inventoryDao;
    @Autowired
    private InventoryBatchDao inventoryBatchDao;

    @Override
    public void process(InventoryOptionDTO dto) {
        //获取入库的ioid
        String ioId = dto.getRefId();
        //查询当前ioid的所有商品
        List<IoItem> ioItems = ioItemDao.select(new IoItemQuery().setIoId(ioId));
        //遍历商品进行库存操作
        ioItems.forEach(ioItem -> {
            //循环开始
            //通过当前商品的ID在库存中查询
            InventoryQuery query = new InventoryQuery().setId(ioItem.getProductId());
            List<Inventory> inventoryList = inventoryDao.select(query);
            //如果库存中没有该商品，则直接添加插入即可
            if (ObjectUtil.isEmpty(inventoryList)) {
                Inventory inventory = new Inventory()
                        .setId(ioItem.getProductId())
                        .setProductName(ioItem.getProductName())
                        .setQty(ioItem.getQty())
                        .setSaleCount(0)
                        .setVersion(0);
                inventoryDao.insert(inventory);
            } else {
                Inventory inventory = inventoryList.get(0);
                //如果库存中有该商品，则直接更新即可
                Integer i = inventoryDao.updateQty(ioItem.getQty(), inventory.getId(), inventory.getVersion());
                if (i == 0) {
                    BizException.throwBizException(453, "库存更新失败");
                }
            }
            //处理批次表
            //生成批次ID（B+商品ID+_+生产日期）
            String batchId = "B" + ioItem.getProductId() + "-" + LocalDateTimeUtil.format(ioItem.getProductionDate(), "yyyyMMdd");
            //在批次表中查询这个批次是否存在
            InventoryBatchQuery batchQuery = new InventoryBatchQuery().setBatchId(batchId);
            List<InventoryBatch> inventoryBatches = inventoryBatchDao.select(batchQuery);
            //如果批次不存在，则直接插入
            if (ObjectUtil.isEmpty(inventoryBatches)) {
                Date date = DateUtil.date(ioItem.getProductionDate().atStartOfDay(ZoneId.systemDefault()).toInstant());
                DateTime dateTime = DateUtil.offsetDay(date, ioItem.getShelfLife());
                InventoryBatch inventoryBatch = new InventoryBatch()
                        .setBatchId(batchId)
                        .setProductId(ioItem.getProductId())
                        .setProductName(ioItem.getProductName())
                        .setQty(ioItem.getQty())
                        .setShelfId(ioItem.getShelfId())
                        .setShelfLife(ioItem.getShelfLife())
                        .setProductionDate(ioItem.getProductionDate())
                        .setBrief(ioItem.getBrief())
                        .setExpirationDate(dateTime.toLocalDateTime().toLocalDate())
                        .setVersion(0);

                inventoryBatchDao.insert(inventoryBatch);
            } else {
                InventoryBatch inventoryBatch = inventoryBatches.get(0);
                //如果批次存在，则直接更新
                Integer i = inventoryBatchDao.updateQty(ioItem.getQty(), inventoryBatch.getBatchId(), inventoryBatch.getVersion());
                if (i == 0) {
                    BizException.throwBizException(454, "批次更新失败");
                }

            }

        });


    }
}
